<?php

require_once('environment.php');
require_once('http_handle_get.php');
require_once('http_handle_post.php');

// regular expressions used in request templates (below)
$PAT_USERNAME = '[0-9a-zA-Z]{2,15}'; // match username
$PAT_INVESTMENT_ID = '[0-9a-z]{13}'; // match any investment ID

// get/invoke function that will be handling request
switch ($_SERVER['REQUEST_METHOD']) {
    case 'GET':
        $resolve = get_url_handler(array(
            '/' => 'http_get_history',
            '/index.php' => 'http_get_history',
            '/upcoming' => 'http_get_upcoming',
            '/upcoming/new' => 'http_get_upcoming_new',
            '/more-investments' => 'http_get_more_investments',
            '/more-upcoming' => 'http_get_more_upcoming',
            '/more-comments' => 'http_get_more_comments',
            "/investment/($PAT_INVESTMENT_ID)" => 'http_get_investment',
            "/avatar/($PAT_USERNAME)" => 'http_get_avatar',
            "/investment-image-full/($PAT_INVESTMENT_ID)" =>
                'http_get_investment_img_full',
            "/investment-image-thumb/($PAT_INVESTMENT_ID)" =>
                'http_get_investment_img_thumb',
            '/sim' => 'http_get_sim'
            ));
        break;
    case 'POST':
        $resolve = get_url_handler(array(
            "/vote/($PAT_INVESTMENT_ID)" => 'http_post_vote',
            '/generate-upcoming' => 'http_post_generate_upcoming',
            '/end-of-month' => 'http_post_end_of_month',
            '/reset-simulation' => 'http_post_reset_simulation',
            '/upcoming/new' => 'http_post_upcoming_new',
            "/like/($PAT_INVESTMENT_ID)" => 'http_post_like',
            "/dislike/($PAT_INVESTMENT_ID)" => 'http_post_dislike',
            "/investment/($PAT_INVESTMENT_ID)" => 'http_post_comment'
            ));
        break;
    default:
        header('HTTP/1.1 405 Method Not Allowed');
        exit;
}

init_environment();

// invoke handler function, with any regex argument matches, and
// print the resultant HTML
extract($resolve);
array_shift($matches); // ignore first match (the request URL)
call_user_func_array($handler, $matches);

/**
 * Return function designated as handler for current request URL. Also
 * returns a regex matches object for the handler pattern.
 *
 * $map = array(
 *     '/foo' => 'http_get_foo',
 *     '/foo/bar' => 'http_get_foobar'
 * );
 *
 * @param array URL pattern to handler function map (see above)
 * @return array handler function and regex matches, as array
 */
function get_url_handler($map) {

    // get lowercase relative request URL, eg /foo/bar
    $request_url = strtolower($_SERVER['PATH_INFO']);

    // return handler associated with matching URL pattern
    foreach ($map as $pattern => $handler) {
        // allow optional trailing /, eg /foo/bar/
        // allow optional query string, eg /foo?bar
        $encoded_pattern = '/^' . str_replace('/', '\/', $pattern) . '\/?(\?.*)?$/';
        if (preg_match($encoded_pattern, $request_url, $matches)) {
            return array(
                'handler' => $handler,
                'matches' => $matches);
        }
    }

    // no handler function was found which means the resource doesn't exist
    http_not_found();
}

/**
 * Respond to client that the resource requested by the HTTP request was
 * not found.
 */
function http_not_found() {
    header('HTTP/1.1 404 Not Found');
    exit;
}

/**
 * Respond to client that the HTTP request was badly formed.
 */
function http_bad_request() {
    header('HTTP/1.1 400 Bad Request');
    exit;
}

?>
